using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
using SqlSugar;

namespace SqlSugarEx.Realization
{
	public class OracleProviderEx : OracleProvider
	{
		private OracleDataAdapter _mySqlAdp;

		private DataSet _dsBind;

		public Func<OracleRowUpdatingEventArgs, string> RowUpdatingHandler { get; set; }

		public override DataTable GetDataTable(string sql, params SugarParameter[] parameters)
		{
			OracleConnection oracleConnection = new OracleConnection
			{
				ConnectionString = this.Connection.ConnectionString
			};
			try
			{
				SqlSugar.IDataAdapter adapter = this.GetAdapter();
				if (adapter is MyOracleDataAdapter)
				{
					OracleCommand oracleCommand = new OracleCommand(sql, oracleConnection);
					foreach (SugarParameter sugarParameter in parameters)
					{
						oracleCommand.Parameters.Add(new OracleParameter(sugarParameter.ParameterName, sugarParameter.Value));
					}
					this._mySqlAdp = new OracleDataAdapter(oracleCommand);
					this._mySqlAdp.MissingSchemaAction = MissingSchemaAction.AddWithKey;
					OracleCommandBuilder oracleCommandBuilder = new OracleCommandBuilder(this._mySqlAdp);
					this._dsBind = new DataSet();
					this._mySqlAdp.Fill(this._dsBind);
					return this._dsBind.Tables[0];
				}
				return new DataTable();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection)
				{
					oracleConnection.Close();
				}
			}
		}

		public bool UpdateBind()
		{
			try
			{
				this._mySqlAdp.Update(this._dsBind);
				return true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
